package org.chen.wordwave.tools.email;

import android.content.Context;
import android.util.Log;

import com.hjq.toast.ToastParams;
import com.hjq.toast.Toaster;
import com.hjq.toast.style.CustomToastStyle;

import org.chen.wordwave.R;

import java.io.UnsupportedEncodingException;
import java.security.SecureRandom;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class EmailUtil {
    static String code = "";
    public static String generateNumericCode(int length) {
        SecureRandom random = new SecureRandom();
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            sb.append(random.nextInt(10));
        }
        return sb.toString();
    }
    public static void sendEmail(Context context, String recipientEmail){
        String username = context.getString(R.string._qq_com);
        String password = context.getString(R.string.wi);
        new Thread(new Runnable() {
            @Override
            public void run() {
                Properties props = new Properties();
                props.put("mail.smtp.auth", "true");
                props.put("mail.smtp.starttls.enable", "true");
                props.put("mail.smtp.host", "smtp.qq.com");
                props.put("mail.smtp.port", "587");
                code = generateNumericCode(6);
                Session session = Session.getInstance(props,
                        new Authenticator() {
                            protected PasswordAuthentication getPasswordAuthentication() {
                                return new PasswordAuthentication(username, password);
                            }
                        });

                try {
                    // 创建邮件消息
                    MimeMessage message = new MimeMessage(session);
                    message.setFrom(new InternetAddress("1905333495@qq.com", "WordMaster - A WordWizard", "utf-8"));
                    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail));
                    message.setSubject(context.getString(R.string.verify_email));

                    // 创建HTML内容
                    String htmlContent = "<html>"
                            + "<body style='background-color:#f4f4f9;padding:20px;font-family:Arial,sans-serif;'>"
                            + "<div style='max-width:600px;margin:auto;background:white;padding:20px;border-radius:10px;box-shadow:0 0 10px rgba(0,0,0,0.1);'>"
                            + "<h2 style='color:#4CAF50;'>WordMaster - A WordWizard</h2>"
                            + "<p style='color:#555;'>"
                            + context.getString(R.string.your_code_is) + " <strong>" + code + "</strong>"
                            + "</p>"
                            + "<p style='color:red'>"
                            + "Please do not inform others to avoid other consequences."
                            + "</p>"
                            + "</div>"
                            + "</body>"
                            + "</html>";

                    // 设置邮件内容
                    message.setContent(htmlContent, "text/html;charset=UTF-8");

                    // 发送邮件
                    Transport.send(message);

                    // 显示成功提示
                    ToastParams toastParams = new ToastParams();
                    toastParams.style = new CustomToastStyle(R.layout.success);
                    toastParams.text = context.getString(R.string.Verification_code_sent);
                    Toaster.show(toastParams);
                } catch (MessagingException | UnsupportedEncodingException e) {
                    Log.e("EmailUtil", e.toString());
                    ToastParams toastParams = new ToastParams();
                    toastParams.style = new CustomToastStyle(R.layout.error);
                    toastParams.text = context.getString(R.string.Verification_code_not_sent);
                    Toaster.show(toastParams);
                }
            }
        }).start();
    }

    public static void sendResetPasswordCode(Context context, String recipientEmail) {
        String username = context.getString(R.string._qq_com);
        String password = context.getString(R.string.wi);
        new Thread(new Runnable() {
            @Override
            public void run() {
                Properties props = new Properties();
                props.put("mail.smtp.auth", "true");
                props.put("mail.smtp.starttls.enable", "true");
                props.put("mail.smtp.host", "smtp.qq.com");
                props.put("mail.smtp.port", "587");
                code = generateNumericCode(6); // Generate a 6-digit code for password reset
                Session session = Session.getInstance(props,
                        new Authenticator() {
                            protected PasswordAuthentication getPasswordAuthentication() {
                                return new PasswordAuthentication(username, password);
                            }
                        });

                try {
                    MimeMessage message = new MimeMessage(session);
                    message.setFrom(new InternetAddress("1905333495@qq.com", "WordMaster - A WordWizard", "utf-8"));
                    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail));
                    message.setSubject(context.getString(R.string.reset_password));

                    // 创建HTML内容
                    String htmlContent = "<html>"
                            + "<body style='background-color:#f4f4f9;padding:20px;font-family:Arial,sans-serif;'>"
                            + "<div style='max-width:600px;margin:auto;background:white;padding:20px;border-radius:10px;box-shadow:0 0 10px rgba(0,0,0,0.1);'>"
                            + "<h2 style='color:#4CAF50;'>WordMaster - A WordWizard</h2>"
                            + "<p style='color:#555;'>"
                            + context.getString(R.string.your_reset_code_is) + " <strong>" + code + "</strong>"
                            + "</p>"
                            + "<p style='color:red'>"
                            + "Please do not inform others to avoid other consequences."
                            + "</p>"
                            + "</div>"
                            + "</body>"
                            + "</html>";

                    // 设置邮件内容
                    message.setContent(htmlContent, "text/html;charset=UTF-8");

                    Transport.send(message);

                    ToastParams toastParams = new ToastParams();
                    toastParams.style = new CustomToastStyle(R.layout.success);
                    toastParams.text = context.getString(R.string.send_pass);
                    Toaster.show(toastParams);
                } catch (MessagingException | UnsupportedEncodingException e) {
                    Log.e("EmailUtil", e.toString());
                    ToastParams toastParams = new ToastParams();
                    toastParams.style = new CustomToastStyle(R.layout.error);
                    toastParams.text = context.getString(R.string.Verification_code_not_sent);
                    Toaster.show(toastParams);
                }
            }
        }).start();
    }

    public static String getCode(){
        return EmailUtil.code;
    }
}
